package com.systop.ltbook.webapp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.systop.cms.CmsConstants;
import com.systop.cms.model.Articles;
import com.systop.cms.model.Catalogs;
import com.systop.cms.webview.service.ArticleFreeMarkerManager;
import com.systop.cms.webview.service.CatalogFreeMarkerManager;
import com.systop.core.webapp.struts2.action.BaseAction;
import com.systop.ltbook.BookConstants;
import com.systop.ltbook.book.service.BookManager;

@SuppressWarnings({ "serial"})
@Controller
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class MainAction extends BaseAction {
	
	//图书类型,首页图书查看类型
	private String bookType;
	
	//首页显示栏目ID
	private Integer catalogId;
	
	//区分教学器材、招生就业
	private String rootPath;
	
	@Autowired
	private ArticleFreeMarkerManager articleMgr;
	
	@Autowired
	private CatalogFreeMarkerManager catalogMgr;
	
	@Autowired
	private BookManager bookMgr;
	
	//首页需要显示数据
	private Map<String, Object> data;
	
	/**
	 * 首页
	 * @return
	 */
	public String index(){
		data = new HashMap<String, Object>();
		data.put(LtConstants.ELITE_INFO, articleMgr.getEliteArticlesInfo(null, 5, getRequest().getContextPath()));
		data.put(LtConstants.NOTICES, articleMgr.getArtsByCatName("公告", 10));
		data.put(LtConstants.HOT_INFOS, articleMgr.getArtsByCatName("热门资讯", 12));
		data.put(LtConstants.SUBJECTS, articleMgr.getEliteArticles("专题", 5));
		return "index";
	}
	
	/**
	 * 首页 -图书信息
	 * @return
	 */
	public String indexBooks(){
		data = new HashMap<String, Object>();
		if (StringUtils.isBlank(bookType)) {
			bookType = BookConstants.TYPE_NEW;
		}
		data.put(LtConstants.IMGBOOKS, bookMgr.getByType(bookType, 12));
		data.put(LtConstants.BOOKS, bookMgr.getBooks(12));
		data.put(LtConstants.TOP10_BOOKS, bookMgr.getTopBooks(10));
		return "index_books";
	}
	
	private Catalogs getCataByRootPath(String rootPath){
		return catalogMgr.findObject("from Catalogs c where c.rootPath=?", rootPath);
	}
	
	/**
	 * 首页-[教学器材]
	 * @return
	 */
	public String indexJxqc(){
		rootPath = "/jxqc"; //用于查询栏目
		queryArticle(rootPath);
		return "index_jxqc";
	}
	
	/**
	 * 首页-[招生就业]
	 * @return
	 */
	public String indexZsjy(){
		rootPath = "/zsjy"; //用于查询栏目
		queryArticle(rootPath);
		return "index_zsjy";
	}
	
	/**
	 * 首页-[会议培训]
	 * @return
	 */
	public String indexHypx(){
		rootPath = "/hypx";
		queryArticle(rootPath);
		return "index_hypx";
	}
	
	//为教学器材、招生就业查询数据
	private void queryArticle(String rootPath){
		data = new HashMap<String, Object>();
		Catalogs cata = getCataByRootPath(rootPath);
		if (cata != null){
			List<Articles> imgArticles = null;
			if (catalogId != null){
				imgArticles = articleMgr.getByCataId(catalogId, 12, CmsConstants.Y, null);
			}else{
				imgArticles = articleMgr.getByCataId(cata.getId(), 12, CmsConstants.Y, null);
			}
			data.put(LtConstants.IMG_ARTICLES, imgArticles);
			data.put(LtConstants.ARTICLES, articleMgr.getByCataId(cata.getId(), 12, null, null));
			data.put(LtConstants.ONTOP_ARTICLES, articleMgr.getByCataId(cata.getId(), 10, null, CmsConstants.Y));
			data.put(LtConstants.SUB_CATALOGS, catalogMgr.getSubCatsById(cata.getId()));
			getRequest().setAttribute("catalogName", cata.getName());
		}
	}
	
	
	public Map<String, Object> getData() {
		return data;
	}

	public String getBookType() {
		return bookType;
	}

	public void setBookType(String bookType) {
		this.bookType = bookType;
	}

	public Integer getCatalogId() {
		return catalogId;
	}

	public void setCatalogId(Integer catalogId) {
		this.catalogId = catalogId;
	}
	
}
